package com.order.manager.util;

import org.springframework.stereotype.Component;

import java.util.Random;
@Component
public class RandomCodeGenerator {
//    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    // public static void main(String[] args) {
    //     String randomCode = generateRandomCode(8);
    //     System.out.println("Random Code: " + randomCode);
    // }
    //generateRandomCode(8)中间是指定位数，默认8位
    //System.out.println("Random Code: " + randomCode);可以用来测试生成的时候有问题
    public  String generateRandomCode(int length) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(randomIndex);
            sb.append(randomChar);
        }

        return sb.toString();
    }
}